﻿using System;
using System.Collections.Generic;
using System.Data;
using Bdcs.Application.Mappers.Dto;
using Bdcs.Application.Services.IServices;
using Bdcs.Core.Entity;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;

namespace Bdcs.Application.Services
{
    public class UserAuthService : IUserAuthService, ITransient
    {
        private readonly IRepository<Adv_AuthInfoMod> _authRepository;

        public UserAuthService(IRepository<Adv_AuthInfoMod> authRepository)
        {
            _authRepository = authRepository;
        }

        public int SaveUserAuth(string userId,string auths)
        {
            var rowEffects =
                @"exec dbo.usp_Adv_UserAuth_Save  @UserId,@Auths "
                    .SqlNonQuery(new
                    {
                        UserId = userId,
                        Auths = auths
                    });
            Console.WriteLine(rowEffects);
            return rowEffects;
        }

        public List<UserAuthDto> List(string userId)
        {
            string mUserID = userId;
            List<UserAuthDto> nodes = new List<UserAuthDto>();
            var query = @"select id,name,UserID from adv_AuthInfo A left join dbo.Adv_UserAuth B
                on B.AuthID = A.ID and b.UserID=@UserId where A.FID='0'".SqlQuery(new
            {
                UserId = mUserID
            });
            
            foreach (DataRow dr in query.Rows)
            {
                UserAuthDto node = GetNode(userId,dr["id"].ToString(), dr["name"].ToString(), !Convert.IsDBNull(dr["UserID"]));
                nodes.Add(node);
            }

            return nodes;
        }
        
        /// <summary>
        /// 构建功能模块树结构方法
        /// </summary>
        /// <param name="userId">对应用户编码</param>
        /// <param name="id">模块主键id</param>
        /// <param name="name">功能模块名称</param>
        /// <param name="haveAuth">是否为空,好像没啥用</param>
        /// <returns></returns>
        private UserAuthDto GetNode(string userId,string id,string name,bool haveAuth)
        {
            UserAuthDto auth = new UserAuthDto(){id=id,name = name,bchecked=haveAuth};
            var query = @"select id,name,UserID from adv_AuthInfo A left join dbo.Adv_UserAuth B
                on B.AuthID = A.ID and b.UserID=@UserId where A.FID=@fid".SqlQuery(new
            {
                UserId = userId,
                fid = id
            });

            if(query.Rows.Count > 0)
            {
                List<UserAuthDto> children = new List<UserAuthDto>();
                foreach(DataRow dr in query.Rows)
                {
                    children.Add(GetNode(userId,dr["id"].ToString(), dr["name"].ToString(), !Convert.IsDBNull(dr["UserID"])));
                }
                auth.isParent =true;
                auth.children = children;
            }
            else
            {
                auth.isParent =false;
                auth.children = null;
            }
       
            return auth;
        }
    }
}